//
//  XDGetSubBankNameController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/3/9.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDGetSubBankNameController.h"
#import "AFHTTPSessionManager+POST.h"
@interface XDGetSubBankNameController ()<UISearchBarDelegate,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UISearchBar *searchBar;

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSArray *subCityNameArr;

@end

@implementation XDGetSubBankNameController

- (NSArray *)subCityNameArr {
    if (_subCityNameArr == nil) {
        _subCityNameArr = [NSArray array];
    }
    return _subCityNameArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    self.view.backgroundColor = kColorRGB(234, 234, 234);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"提现须知"  style:UIBarButtonItemStylePlain target:self action:@selector(rightItem)];
    rightItem.tintColor = kColorRGB(187,147,115);
    self.navigationItem.rightBarButtonItem = rightItem;
    [self setupUI];
    
    [self loadData];
    
}

- (void)loadData {
    self.bankAccountNo = self.bankAccountNo ? self.bankAccountNo : @"";
    self.cityCode = self.cityCode ? self.cityCode : @"";
    self.bankCode = self.bankCode ? self.bankCode : @"";
    NSDictionary * param = @{@"method":@"getSubBankName",@"cityCode":self.cityCode,@"bankCode":self.bankCode,@"bankAccountNo":self.bankAccountNo};
    
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            
            /*
             {
             "type": "success",
             "code": "100000",
             "message": "操作成功",
             "result": {
             "bfSubbankIdentityList": [
             {
             "version": 0,
             "subBankName": "中国农业银行股份有限公司北京永安里支行",
             "display": "[null]BfSubbankIdentity"
             }
             ]
             }
             }
             */
            
            self.subCityNameArr = responseObject[@"result"][@"bfSubbankIdentityList"];
            
            [self.tableView reloadData];
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}


- (void)setupUI {
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 43)];
    searchBar.placeholder = @"请输入银行支行";
    searchBar.delegate = self;
    searchBar.backgroundColor = kColorRGB(234, 234, 234);
    self.searchBar = searchBar;
    [self.view addSubview:searchBar];
    
    UITableView *tablew = [[UITableView alloc] initWithFrame:CGRectMake(0,43, kScreenW, kScreenH - 43 - 64)];
    tablew.delegate = self;
    tablew.dataSource = self;
    tablew.showsVerticalScrollIndicator = NO;
    tablew.showsHorizontalScrollIndicator = NO;
    tablew.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.tableView = tablew;
    [self.view addSubview:tablew];
    tablew.backgroundColor = kColorRGB(234, 234, 234);

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 10)];
    tablew.tableFooterView = view;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.subCityNameArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"getSubCityCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
    }
    cell.textLabel.text = self.subCityNameArr[indexPath.row][@"subBankName"];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"subCityName" object:nil userInfo:self.subCityNameArr[indexPath.row]];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(nonnull NSString *)searchText {
    NSString *searchStr = [[self.searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@""] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [self searchCityLoadData:searchStr];
}
- (void)searchCityLoadData:(NSString *)searchBarText{
    
    NSLog(@"搜索");
    
    [self loadSearchData:searchBarText];
    
}
- (void)loadSearchData:(NSString *)searchStr {
    if (searchStr.length > 0) {
        NSDictionary * param = @{@"method":@"fastSearchSubBankName",@"cityCode":self.cityCode,@"name":searchStr,@"bankCode":self.bankCode};
        [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([responseObject[@"code"] isEqualToString:@"100000"]) {
                /*
                 
                 {
                 display = "[null]BfSubbankIdentity";
                 subBankName = "\U4e2d\U56fd\U90ae\U653f\U50a8\U84c4\U94f6\U884c\U80a1\U4efd\U6709\U9650\U516c\U53f8\U5317\U4eac\U987a\U4e49\U533a\U56fd\U95e8\U652f\U884c";
                 version = 0;

                 张路路  14:46:21
                 fastSearchSubBankNam
                 */
                self.subCityNameArr = responseObject[@"result"][@"bfSubbankIdentityList"];
                [self.tableView reloadData];
                
            } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
                
                
            } else {
                
                
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    }
   
}



- (void)rightItem {
    
    NSLog(@"提现须知");
    
}

@end
